<?php
/**
 * cruise.php
 *
 * cruise类
 * @author konakona
 * @date 2009-10-12
 */
class cruise {
/**
 * 添加航班时间
 * @param string $_post
 */
	public static function cruise_add($_post) {
		if($_post['flight']=='') {
			k::message('请选择油轮','javascript:history.go(-1);');
		}
		if($_post['city_id']=='') {
			k::message('请选择路线','javascript:history.go(-1);');
		}
		$sql = "INSERT INTO cruise  values('','{$_post[week]}','{$_post[time]}','{$_post[flight]}','{$_post[city_id]}')";
		mysql::connect()->query($sql);
		k::message('航班已添加！','javascript:history.go(-1);');
	}
	/**
	 * 添加航班路线
	 * @param string $_post
	 */
	public static function cruise_route_add($_post) {
		if($_post['start']==$_post['end']) {
			k::message('出发点与抵达点不能一样','javascript:history.go(-1);');
		}
		$sql = "INSERT INTO cruise_city  values('','{$_post[start]}','{$_post[end]}','{$_post[way]}')";
		mysql::connect()->query($sql);
		k::message('航线已经添加，您现在可以<a href="admin.php?action=cruise-add">添加航班时间</a>了！','javascript:history.go(-1);');
	}
	/**
	 * 提取出固定路线，并制作成固定格式后返回
	 * @return array
	 */
	public static function city_list($id=false) {
		if($id) {$sql = "select * from cruise_city,cruise where cruise.fid=$id and cruise.city_id = cruise_city.city_id";}else {$sql = "select * from cruise_city";}
		$result = mysql::connect()->fetch_all($sql);
		$i = 0;
		foreach($result as $k=>$v) {
			if($v['Updown']==1) {$Updown = 'Upstream';}else {$Updown = 'Downstream';}
			if($id) {
				$data[$i]['city'] = $v['Begin_city'].">>".$v['End_city'].",".$Updown.",".self::NumToweek($v['Begin_date']);
			}else {
				$data[$i]['city'] = $v['Begin_city'].">>".$v['End_city'].",".$Updown;
			}
			$data[$i]['city_id'] = $v['city_id'];//赋予航线ID
			$data[$i]['Begin_date'] = $v['Begin_date'];//赋予周期
			$i +=1;
		}
		return $data;
	}
	/**
	 * 将数据转移到目录并返回存sql字符串
	 * @param string $name array $_FILES
	 * @return stirng
	 */
	public static function movieFile($name,$_FILES) {
		$ImageDir = "pic/";
		if(!is_dir($ImageDir))
			k::message('你并没有pic这个文件夹，请建立后再尝试上传','javascript:history.go(-1);');
		$str = '';
		foreach($_FILES[$name]['tmp_name'] as $key=>$value) {
			$tmp_name = md5($_FILES[$name]['name'][$key].time()).substr($_FILES[$name]['name'][$key],-4,4);
			copy($_FILES[$name]['tmp_name'][$key],$ImageDir.$tmp_name);
			$str.= $tmp_name."|";

		}
		return $str;
	}
	/**
	 * 删除cruise
	 * @param mixed $id
	 */
	public static function cruise_del($id) {
		$sql = "DELETE FROM cruise WHERE cid=$id";
		mysql::connect()->query($sql);
		k::message('这条cruise已删除成功','javascript:history.go(-1);');
	}
	/**
	 * 列出所有cruise
	 * @param int $pid $row
	 * @return array
	 */
	public static function cruise_list($pid,$row=10) {
		if($pid == 0) {
			$pid = 1;
		}
		$allcount = page::tableCount('cruise');
		if($pid==1) {
			$sql = "select * from cruise,flight,cruise_city where  flight.fid = cruise.fid and cruise.city_id = cruise_city.city_id limit ".($pid-1).",$row";
		}else {
			$page_start  = ($pid-1)+$row;
			$sql = "select * from cruise,flight,cruise_city where  flight.fid = cruise.fid and cruise.city_id = cruise_city.city_id limit ".($page_start-1).",$row";
		}
		return mysql::connect()->fetch_all($sql);
	}
	/**
	 * 顯示當前月的航班及游轮信息
	 *
	 * @param int $year
	 * @param int $year
	 * @return array
	 */
	public static function cruise_month($year,$month) {
		$sql = "select * from cruise,flight where cruise.fid = flight.fid order by Begin_date";
		return mysql::connect()->fetch_all($sql);
	}
	/**
	 * 判斷季節。
	 *
	 */
	public static function getWJ($month,$id=0) {
		if($id==0) {
			$sql = "select fid from flight where shoulder_seasons like '%{$month}%'";
		}else {
			$sql = "select fid from flight where shoulder_seasons like '%{$month}%' and fid=$id";
		}
		$result = mysql::connect()->fetch_all($sql);
		if($result[0]['fid']=='') {
			$sql = "select fid from flight where high_seasons like '%{$month}%'";
			$result = mysql::connect()->fetch_all($sql);
			$ji[0] = 'hs_';
			$ji[1] = 'high_seasons';
		}else {
			$ji[0] = 'sh_';
			$ji[1] = 'shoulder_seasons';
		}
		return $ji;
	}
	/**
	 * 通过日期和航线获取邮轮的id
	 * @param int $date
	 * @param array $cruise 
	 */
	public static function cruise_getFID($date,$cruise){
	    $date = explode("-",$date);
	    $time = mktime(0,0,0,$date[1],$date[2],$date[0]);
	    $week = date("N",$time);
	    $cruise = explode(",",$cruise);
	    $cruise = explode(">>",$cruise[0]);
	    $Begin_city = $cruise[0];
	    $End_city = $cruise[1];
	    $city_result = mysql::connect()->fetch_all("select city_id,Updown from cruise_city where Begin_city = '{$Begin_city}' and End_city = '{$End_city}'");
	    $result = mysql::connect()->fetch_all("select fid from cruise where city_id = {$city_result[0][city_id]} and Begin_date = {$week}");
	    $result[0]['updown'] = $city_result[0]['Updown'];
	    return $result;
	}
	/**
	 * 根据年和月，查出季节，并返回邮轮数据
	 * @param <type> $fid
	 * @param <type> $month
	 * @param <type> $year
	 * @param <type> $updown
	 */
	public static function search($fid,$month,$year,$updown){
	    $jj = self::getWJ($month,$fid);
	    //拼凑上下行
	     $up = $jj[0].'downstream';
	     $down =  $jj[0].'upstream';
	    $tiaojian = "{$up} as up,{$down} as down,informations,stars,fname,flight.fid,Begin_time,cruise_city.Begin_city,cruise_city.End_city";
	    $sql = "select {$tiaojian} from flight,cruise,cruise_city where `{$jj[1]}` like '%$month%' and flight.fid = $fid and cruise.fid = flight.fid and cruise_city.city_id = cruise.city_id";
	    $result = mysql::connect()->fetch_all($sql);
	    return $result[0];
	}
	/**
	 * 通过数字转换为月份
	 * @param int $num
	 */
	public static function NumTomonth($num) {
		switch($num) {
			case '1':
				$cal = 'January';
				break;
			case '2':
				$cal = 'February';
				break;
			case '3':
				$cal = 'March';
				break;
			case '4':
				$cal = 'April';
				break;
			case '5':
				$cal = 'May';
				break;
			case '6':
				$cal = 'June';
				break;
			case '7':
				$cal = 'July';
				break;
			case '8':
				$cal = 'August';
				break;
			case '9':
				$cal = 'September';
				break;
			case '10':
				$cal = 'October';
				break;
			case '11':
				$cal = 'November';
				break;
			case '12':
				$cal = 'December';
				break;
		}
		return $cal;
	}
	/**
	 * 通过数字转换为月份
	 * @param int $num
	 */
	public static function NumToweek($num) {
		switch($num) {
			case '1':
				$day = 'Monday';
				break;
			case '2':
				$day = 'Tuesday';
				break;
			case '3':
				$day = 'Wednesday';
				break;
			case '4':
				$day = 'Thursday';
				break;
			case '5':
				$day = 'Friday';
				break;
			case '6':
				$day = 'Saturday';
				break;
			case '7':
				$day = 'Sunday';
				break;
		}
		return $day;
	}
	/**
	 * 通过星期英文转换为数字
	 * @param int $week
	 */
	public static function WeekTonum($week) {
		switch($week) {
			case 'Monday':
				$day = '1';
				break;
			case 'Tuesday':
				$day = '2';
				break;
			case 'Wednesday':
				$day = '3';
				break;
			case 'Thursday':
				$day = '4';
				break;
			case 'Friday':
				$day = '5';
				break;
			case 'Saturday':
				$day = '6';
				break;
			case 'Sunday':
				$day = '7';
				break;
		}
		return $day;
	}
	/**
	 * 生成日曆
	 *
	 * @param int $month
	 * @param int $data
	 * @param int $year
	 * @return mexid
	 */
	public static function cruise_html($month,$data,$year=2009) {
		$classname = strtolower(date("l",mktime(0,0,0,$month,1,$year)));//获取该月第一天的英文星期
		$month_days = cal_days_in_month(CAL_GREGORIAN,$month,$year);
		$num = array();
		for($j=0;$j<12;$j++) {
			if(isset($data[$j]['Begin_date'])) {
				$num[] .= $data[$j]['Begin_date'];
			}
		}
		$i=0;
		foreach($data as $key=>$value) {
			$temp_data[$num[$i]] = ($data[$key]);
			$i +=1;
		}
		for($i=1;$i<$month_days+1;$i++) {
			$real_day = $i;
			if(isset($temp_data[$i])) {
				$result = self::returnData($i);
			}else {
				$result = '';
			}
			if(!empty($result)) {
				if($i==1) { $str = "<li class='{$classname} data left down'>";	}else { $str .= "<li class='data left down'>";}
				$str .= "<div class='today'><span style='z-index:1'>{$real_day}</span><ul class='calDayShort'>";
				$str .="<li>{$temp_data[$i][fname]}<br>{$temp_data[$i][stars]} Stars</li>
</ul><div class='more'> More &gt;&gt;</div>
</div><div style='display: none;' class='calDayData'>
<span style='display: block;' class='close'>Close</span>
<h4>{$classname},{$real_day} {$year}</h4>
<div class='calDayDetail'>";
				foreach($result as $value) {
					$str.="<div class='education'>";
					$str.="<a href='ship-show-{$value[fid]}.html'>{$value[fname]}</a><strong>";
					for($j=0;$j<$temp_data[$i][stars];$j++) {
						$str .="<img src='templates/default/images/star2.gif' width='20' height='20'>";
					}
					$str .="</strong>{$value[itineraries]}<p class='detail'><a href='buy-cruise-{$value[fid]}.html'>Order this ship</a><span class='status'></span></p>";
					$str .="</div>";
				}
				$str .="</div></div>";
			}else {
				$str .= "<li><div>$real_day</div></li>";
			}
		}$str .="</li>";
		return $str;
	}
	public static function returnData($time) {
		$result = mysql::connect()->fetch_all("select * from cruise,flight where Begin_date = '{$time}' and cruise.fid = flight.fid order by Begin_date");
		return $result;
	}
	/**
	 * 读取一条cruise信息
	 * @param int $id
	 * @return array
	 */
	public static function cruise_details($id) {
		$sql = "select * from cruise where cid=$id";
		$result = mysql::connect()->fetch_all($sql);
		return $result[0];
	}
	/**
	 * 修改一条cruise信息
	 * @param int $_post
	 */
	public static function cruise_edit($_post) {
		if($_post['flight']=='') {
			k::message('请选择油轮','javascript:history.go(-1);');
		}
		if($_post['city_id']=='') {
			k::message('请选择路线','javascript:history.go(-1);');
		}
		$sql = "UPDATE `cruise` SET `Begin_date` = '{$_post[week]}',`Begin_time` = '{$_post[time]}',`fid` = '{$_post[flight]}',`city_id` = '{$_post[city_id]}'  where cid={$_post[id]}";
		mysql::connect()->query($sql);
		k::message('修改成功','?action=cruise-list');
	}
	/*
	* 将数据库返回的信息，核对处理后，生成日历。
	* @ string $ym, array $data
	* @return string
	*/
	public static function calendar($ym,$data=false,$id=false,$weekstr=false) {
		if($ym) {
			$year = substr($ym,0,4);
			$month = substr($ym,4,(strlen($ym)-4));

			if($month>12) {
				$year += floor($month/12);
				$month = $month % 12;
			}
			if($year > 2010) $year = 2010;
			if($year < 2009) $year = 2009;
		}
		$year = isset($year) ? $year : date('Y');
		$month = isset($month) ? $month : date('n');

		if($year==date('Y') && $month==date('n')) $today = date('j');

		//如果月-1=0，那么年-1，并且是上一年的12月
		if($month-1 == 0)
			$prevmonth = ($year - 1)."12";
		else $prevmonth = $year.($month - 1);

		if($month+1 == 13)
			$nextmonth = ($year+1)."1";
		else $nextmonth = $year.($month+1);
		if($year ==2009 && $month == date('n')) {$prevmonth=$year.date('n');}
		$prevyear = ($year - 1).$month;
		$nextyear = ($year + 1).$month;
		$html = "
			<table width='200' border='0' cellpadding='2' cellspacing='2'>
	  <tr>
		<td class='weekday'><a href='javascript:void(0)' onclick='prevyear($prevyear)'><<</a></td>
		<td class='normalday'><a href='javascript:void(0)' onclick='prevmonth($prevmonth)'><</a></td>
		<td colspan='3' class='normalday'><span id='yearandmonth'>{$year}-{$month}</span></td>
		<td class='normalday'><a href='javascript:void(0)' onclick='nextmonth($nextmonth)'>></a></td>
		<td class='weekday'><a href='javascript:void(0)' onclick='nextyear($nextyear)'>>></a></td>
	  </tr>
	  <tr>
		<td width='27' class='weekday'>Sun</td>
		<td width='27' class='normalday'>Mon</td>
		<td width='27' class='normalday'>Tue</td>
		<td width='27' class='normalday'>Wed</td>
		<td width='27' class='normalday'>Thur</td>
		<td width='27' class='normalday'>Fri</td>
		<td width='27' class='weekday'>Sat</td>
	  </tr>";
		$nowtime = mktime(0,0,0,$month,1,$year);//当月１号转为秒
		$daysofmonth = date(t,$nowtime);//当月天数
		$weekofbeginday = date(w,$nowtime);//当月第一天是星期几
		$weekofendday = date(w,mktime(0,0,0,$month+1,0,$year));//当月最后一天是星期几
		$daysofprevmonth = date(t,mktime(0,0,0,$month,0,$year));//上个月天数

		$count = 1;//计数

		//如果没有提供星期，那么不进行默认排序动作
		if($weekstr==false) {
			foreach($data as $k=>$v) {
				$week[date('N',mktime(0,0,0,$month,$v['Begin_date'],$year))]=date('N',mktime(0,0,0,$month,$v['Begin_date'],$year));
			}
			ksort($week);
		}

		//列出上月后几天
		for($i = 1 ; $i <= $weekofbeginday ; $i++) {
			$html .="<td class='othermonth'>".($daysofprevmonth-$weekofbeginday+$i)."</td>";
			$count++;
		}
		//当月全部
		for($i = 1 ; $i <= $daysofmonth ; $i++) {
			$css = ($count%7==0 || $count%7==1)?"weekday":"normalday";
			if($i == $today) $css .= "today";
			$nowweek = date('N',mktime(0,0,0,$month,$i,$year));
			if($weekstr == false) {
				if($week[$nowweek]!=date('N',mktime(0,0,0,$month,$i,$year))) {
					$html .= "<td class='{$css}'>".$i."</td>";
				}else {
					$html .= "<td class='".$css."'><a href='javascript:void(0)' title='{$data[$i][city]}' onclick='KO_ForInput(this)'>".$i."</a></td>";
				}
			}else {
				if($weekstr!=date('N',mktime(0,0,0,$month,$i,$year))) {
					$html .= "<td class='{$css}'>".$i."</td>";
				}else {
					$html .= "<td class='".$css."'><a href='javascript:void(0)' title='{$data[$i][city]}' onclick='KO_ForInput(this)'>".$i."</a></td>";
				}
			}
			if($count%7==0) $html .= "</tr><tr>";
			$count++;
		}
		//下月前几天
		for ($i = 1;$i <= 6-$weekofendday;$i++) {
			$html .=     "<td class='othermonth'>".$i."</td>";
		}

		$html.="<tr><td colspan='7'></td></tr></table>";
		return $html;
	}
	/**
	 * 检测搜索数据是否合法避免sql弹出错误
	 * @@param array $post_data
	 */
	public static function check_search($post_data){
		if(!preg_match("/(\w)+(>>){1}(\w+)/",$post_data['city'])) header('Location: index.html');
		if(!preg_match("/(\d){4}\-(\d){1,2}\-(\d){1,2}/",$post_data['date'])) header('Location: index.html');
		//if(!preg_match("/(\w)+(>>){1}(\w+)/",$post_data['quotation'])) header('Location: index.html');
	}
}